
package com.dtguai.app.api.controller.user;

import com.dtguai.app.annotation.Login;
import com.dtguai.app.api.form.login.LoginForm;
import com.dtguai.app.base.action.BaseAction;
import com.dtguai.app.common.error.ErrorCode;
import com.dtguai.app.common.validator.BaseAssert;
import com.dtguai.app.common.validator.ValidatorUtils;
import com.dtguai.app.common.validator.group.AddGroup;
import com.dtguai.app.common.validator.group.LoginGroup;
import com.dtguai.app.model.User;
import com.dtguai.app.protocol.response.ApiResponse;
import com.dtguai.app.service.UserService;
import com.dtguai.app.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author guo
 * @date 2019年2月1日16:26:09
 */
@RestController()
@RequestMapping("/api")
@Api(value = "APP登录接口", tags = {"登录接口"})
@AllArgsConstructor
public class LoginApi extends BaseAction {

    private final UserService userService;

    private final JwtUtils jwtUtils;

    @ApiOperation(value = "登录", notes = "用户登陆")
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    @ApiResponses({@io.swagger.annotations.ApiResponse(code = 10001, message = "手机号或密码错误"),
            @io.swagger.annotations.ApiResponse(code = 1021, message = "token创建失败")})
    public ResponseEntity<ApiResponse<User>> login(@RequestBody LoginForm userFrom) {
        // 表单校验
        ValidatorUtils.validateEntity(userFrom, LoginGroup.class);

        User user = userService.login(userFrom.getMobile(), userFrom.getPassword());

        BaseAssert.isNull(user, ErrorCode.USER_NOT_EXIST_ERROR);

        // 重置imei
        user.setImei(userFrom.getImei());
        // 创建token
        user.setToken(jwtUtils.generateToken(user.getId()));
        user.setExpire(jwtUtils.getExpire());
        return ResponseEntity.ok(new ApiResponse<>(user));
    }

    /**
     * 目前其实不需要登出功能 但是有个续签功能 稍后此方法会改成续签
     *
     * @return ResponseEntity 用户id
     * @date 2019年6月25日15:29:38
     */
    @Login
    @ApiOperation(value = "登出", notes = "用户登出")
    @GetMapping(value = "/logout", produces = "application/json;charset=UTF-8")
    public ResponseEntity<ApiResponse<Object>> logout() {
        return ResponseEntity.ok(new ApiResponse<>(getUserId()));
    }

    @ApiOperation(value = "注册", notes = "注册")
    @PostMapping(value = "/register", produces = "application/json;charset=UTF-8")
    public ResponseEntity<ApiResponse<User>> register(LoginForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form, AddGroup.class);

        User userAdd = new ModelMapper().map(form, User.class);
        userService.insertSelective(userAdd);
        // 创建token
        userAdd.setToken(jwtUtils.generateToken(userAdd.getId()));
        userAdd.setExpire(jwtUtils.getExpire());
        return ResponseEntity.ok(new ApiResponse<User>(userAdd));
    }

    @Login
    @ApiOperation(value = "返回登录用户信息", notes = "返回登录用户信息")
    @GetMapping(value = "/user", produces = "application/json;charset=UTF-8")
    public ResponseEntity<ApiResponse<User>> loginInfo() {
        return ResponseEntity.ok(new ApiResponse<>(getUserInfo()));
    }

    @Login
    @ApiOperation(value = "只获取用户id", notes = "只获取用户id")
    @GetMapping(value = "/user-id", produces = "application/json;charset=UTF-8")
    public ResponseEntity<ApiResponse<Integer>> getUser() {
        return ResponseEntity.ok(new ApiResponse<>(getUserId()));
    }
}
